package com.app114.app.android.rk;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.app114.app.android.rk.bean.WebSiteItem;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.utils.DataFactory;
import com.app114.app.android.rk.widget.ExtendedViewPager;
import com.app114.app.android.rk.widget.HomePagerInterface;
import com.app114.app.android.rk.widget.TitleProvider;

public class WebActivity extends BaseFragmentActivity implements Callback {

	private MyAdapter mAdapter;
	private ExtendedViewPager mPager;
	private TitleProvider mTabs;
	private List<WebSiteItem> gridWebs;
	private Handler mHandler;

	final class MyAdapter extends FragmentPagerAdapter implements HomePagerInterface {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return gridWebs.size();
		}

		@Override
		public Fragment getItem(int position) {
			return new WebFragment(gridWebs.get(position), mHandler);
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public String getTitle(int position) {

			final int len = gridWebs.size();

			if (position >= 0 && position < len)
				return gridWebs.get(position).getName();
			else
				return "";

		}
	}

	@Override
	protected void onDestroy() {
		// mWebView.startAnimation(rightOut);
		super.onDestroy();
	}

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		Intent intent = getIntent();
		Bundle b;
		if (intent == null || (b = intent.getExtras()) == null) {
			return;
		}
		mHandler = new Handler(this);
		setContentView(R.layout.activity_browser);
		int pos = b.getInt("pos");
		String title = b.getString("title");
		gridWebs = DataFactory.getWebSiteList(title);

		mPager = (ExtendedViewPager) findViewById(R.id.pager);
		mPager.setPageMargin(10);
		mTabs = (TitleProvider) findViewById(R.id.tabs);

		mAdapter = new MyAdapter(getSupportFragmentManager());
		mPager.setAdapter(mAdapter);
		mTabs.setViewPager(mPager);
		mPager.setFilterTouchesWhenObscured(false);
		mPager.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);
		mPager.setCurrentItem(pos);
	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_web, menu);
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_fullScreen:
			Intent intent = new Intent(mContext, BrowserActivity.class);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(Conf.currentUrl));
			startActivity(intent);
			break;

		default:
			break;
		}
		return true;
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case Conf.START:
			setSupportProgress(Window.PROGRESS_END);
			setSupportProgressBarIndeterminateVisibility(true);
			break;
		case Conf.FINISHED:
			setSupportProgressBarIndeterminateVisibility(false);
			break;

		default:
			break;
		}
		return false;
	}
}
